package cn.wsalix.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.RelatedServiceImpl;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.admin.entity.RoleMenu;
import cn.wsalix.admin.entity.SysMenu;
import cn.wsalix.admin.entity.SysRole;
import cn.wsalix.admin.form.RoleMenuForm;
import cn.wsalix.admin.repository.RoleMenuRepository;
import cn.wsalix.admin.service.MenuService;
import cn.wsalix.admin.service.RoleMenuService;
import cn.wsalix.admin.service.RoleService;

@Service("roleMenuService")
public class RoleMenuServiceImpl
		extends
		RelatedServiceImpl<RoleMenuForm, RoleMenuRepository, RoleMenu,  SysRole,SysMenu>
		implements RoleMenuService {
	@Autowired
	private MenuService menuService;
	@Autowired
	private RoleService roleService;

	@Autowired
	private RoleMenuRepository roleMenuRepository;

	@Override
	public SysRole findPreFix(Long preFixId) {
		return roleService.findById(preFixId);
	}

	@Override
	public SysMenu findPostFix(Long postFixId) {
		return menuService.findById(postFixId);
	}
	@Transactional
	@Override
	public void removeRelated(RoleMenuForm form) {
		if(form.getId()!=null){
			roleMenuRepository.delete(form.getId());
		}else{
			List<RoleMenu> roleMenus=findByForm(form);
			roleMenuRepository.delete(roleMenus);
		}
		
	}
	//@Cacheable(value = "myMenuCache",key = "#userId")
	@Transactional
	@Override
	public RoleMenu setRelated(RoleMenuForm form) {
		roleService.upUserRole(form.getRoleId());
		return super.setRelated(form);
	}


	@Override
	public List<RoleMenu> findRelateds(RoleMenu entity) {
		if (entity.getPreFixId() != null && entity.getPreFixId() != 0) {
			return roleMenuRepository.findByRoleId(entity.getPreFixId());
		}
		if (entity.getPostFixId() != null && entity.getPostFixId() != 0) {
			return roleMenuRepository.findByMenuId(entity.getPostFixId());
		}
		return null;
	}

	@Override
	public List<SysRole> findUnPreFix(Long menuId) {
		return roleService.findUnRolesByMenuId(menuId);
	}

	@Override
	public List<SysMenu> findUnPostFix(Long roleId) {
		return menuService.findUnMenusByRoleId(roleId);
	}

	@Override
	public RoleMenuRepository getRepository() {
		return roleMenuRepository;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(RoleMenuForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getRoleId() != null && form.getRoleId() != 0) {
			filters.put("role.id", new SearchFilter("role.id",
					Operator.EQ, form.getRoleId()));
		}
		if (form.getMenuId() != null && form.getMenuId() != 0) {
			filters.put("menu.id", new SearchFilter("menu.id.id",
					Operator.EQ, form.getMenuId()));
		}
		return filters;
	}

	@Override
	public String module() {
		return "/admin/role_menu";
	}

	@Override
	public Class<RoleMenu> getEntityClazz() {
		return RoleMenu.class;
	}

}
